var table;
var layer;
var upload;
var form;
var subImg;
layui.use(['layer','table','upload','form'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;

    //上传图片
    upload.render({
        elem : '#upload_btn',
        url : '/SyOperationController/uoloadImg',//上传接口
        done : function(res) {
            if(res.code==0){

                var url=$("#mainImage").val();
                if(url==""||url==null||url==undefined){
                    subImg=res.src;
                    //主图截取
                    var arrayImgs=res.src
                    var url=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
                    $("#mainImage").val(url);
                }

                //获取预览图个数
                var uploadImgNum=document.getElementsByClassName("uploadImg").length;

                //判断预览图是否大于8张，大于8张隐藏
                if(uploadImgNum>7){

                    //预览图计数
                    var num=$("#num").val();
                    if(num==0){
                        //隐藏之后，判断预览图是否大于8张，大于8张添加......
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#h1_id").replaceWith("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }
                    }else{
                        //判断......是否有，在进行块标签或隐藏
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: block'/>");
                        }

                    }
                }else{
                    //添加预览图，加父边框，变成行内块标签
                    $("#img_div2").attr("style","display:block");
                    $("#img_div4").append("<img src='"+res.src+"' class='uploadImg'/>");
                    $("#img_div4").css({"border":"1px #D7D7D7 solid"});
                }
            }
        },
        drag : true,
        accept : 'file',
        multiple : true,
        data : { }
    });





});

//返回
function go() {
    history.go(-1);
}

//选择分类
function addProductCategory() {
    layer.open({
        type:2,
        title:'选择分类',
        area:['640px', '560px'],
        offset:'auto',
        shade:0.5,
        maxmin:true,
        content:"ProductCategory.html",
        success:function(layero,index){
            $("#p_id").attr("style","display:block");
        }
    });
    window.layer_1=layer;
}


//预览图点击展开
function uploadImg() {
    $("#up").attr("style","display:block");
    //预览图点击计数
    var num=0;
    num++;
    $("#num").val(num);
    $(".uploadImg2").attr("style","display:block");
    $("h1").remove(".h");
}

//预览图
function upUploadImg() {
    $(".uploadImg2").attr("style","display:none");
    $("#up").attr("style","display:none");
    $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
}

//添加商品
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){

        var mainImage=$("#mainImage").val();
        if(mainImage==""||mainImage==null||mainImage==undefined){
            layer.msg("请先添加商品图片", {icon: 5});
        }else{
            //处理预选图
            var str=[];
            $("#img_div4 img").each(function (a) {

                //获取图片后面路径 截取拼接
                var arrayImgs= ($(this).attr("src"));
                var img=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);

                if(subImg!=arrayImgs){

                    var create=localStorage.getItem("user");
                    create=JSON.parse(create);//将获取json字符串转json对象

                    var productName=$("#productName").val();
                    json={"pictureType":1,"pictureUrl":img,"pictureName":productName,"createUser":create.user_id};
                    str.push(json);
                }

            });

            //获取预览图
            var formData = data.field;

            //所有图片
            formData.str=JSON.stringify(str);
            //创建人
            var user = parse("user");
            var uId = user.user_id;
            formData.createUser=JSON.stringify(uId);


            $.ajax({
                url:"/SyOperationController/addProduct",
                type:"post",
                dataType:"json",
                traditional: true,//ajax传递数组必须添加属性
                data:formData,
                success:function(data){
                    if(data.code==0){
                        layer.msg('添加成功',{icon:6});
                        history.go(-1);
                    }else{
                        layer.msg('添加失败',{icon:5});
                    }
                }
            });
        }


       return false;

    });

});